import java.util.ArrayList;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2024-12-16
 * Time: 12:37
 */
public class test1 {
    /*
    NewCoder BM46  最小的k个数
     */
    public ArrayList<Integer> GetLeastNumbers_Solution (int[] nums, int k) {
        // write code here
        ArrayList<Integer> list = new ArrayList<Integer>();
        if(nums.length==0 || k==0){
            return list;
        }
        qsort(nums,0,nums.length-1);
        for(int i=0;i<k;i++){
            list.add(nums[i]);
        }
        return list;
    }

    public void qsort(int[] nums,int l,int r){
        if(l>=r){
            return;
        }
        int key = nums[new Random().nextInt(r-l+1)+l];
        int left = l-1;
        int right = r+1;
        int i = l;
        while(i<right){
            if(nums[i]<key){
                swap(nums,++left,i++);
            }else if(nums[i]==key){
                i++;
            }else{
                swap(nums,--right,i);
            }
        }

        qsort(nums,l,left);
        qsort(nums,right,r);
    }

    public void swap(int[] nums,int left,int right){
        int tmp = nums[left];
        nums[left] = nums[right];
        nums[right] = tmp;
    }
}
